<?php

require_once('../includes/SmartyManager.class.php');
require_once('../includes/DB.class.php');
require_once('../includes/Verifier.class.php');
require_once('../includes/Filter.class.php');

ini_set("max_execution_time",0);
ini_set( "memory_limit" , "128M");

$smarty = SmartyManager::getSmarty();
$smarty->assign('advancedSearch', true);

if(isset($_GET['certificationNumber'])){
	$smarty->assign('certificationNumber', $_GET['certificationNumber']);
	$_GET['certificationNumber'] = Filter::forDBInsertion(trim($_GET['certificationNumber']));
}

if(isset($_GET['facheNumber'])){
	$smarty->assign('facheNumber', $_GET['facheNumber']);
	$_GET['facheNumber'] = Filter::forDBInsertion(trim($_GET['facheNumber']));
}

if(isset($_GET['size1From'])){
	$smarty->assign('size1From', $_GET['size1From']);
	$_GET['size1From'] = trim($_GET['size1From']);
}
if(isset($_GET['size1To'])){
	$smarty->assign('size1To', $_GET['size1To']);
	$_GET['size1To'] = trim($_GET['size1To']);
}
if(isset($_GET['size2From'])){
	$smarty->assign('size2From', $_GET['size2From']);
	$_GET['size2From'] = trim($_GET['size2From']);
}
if(isset($_GET['size2To'])){
	$smarty->assign('size2To', $_GET['size2To']);
	$_GET['size2To'] = trim($_GET['size2To']);
}
if(isset($_GET['size3From'])){
	$smarty->assign('size3From', $_GET['size3From']);
	$_GET['size3From'] = trim($_GET['size3From']);
}
if(isset($_GET['size3To'])){
	$smarty->assign('size3To', $_GET['size3To']);
	$_GET['size3To'] = trim($_GET['size3To']);
}
if(isset($_GET['uploadTimeFrom'])){
	$smarty->assign('uploadTimeFrom', $_GET['uploadTimeFrom']);
	$_GET['uploadTimeFrom'] = trim($_GET['uploadTimeFrom']);
}
if(isset($_GET['uploadTimeTo'])){
	$smarty->assign('uploadTimeTo', $_GET['uploadTimeTo']);
	$_GET['uploadTimeTo'] = trim($_GET['uploadTimeTo']);
}

if(isset($_GET['rukuDateFrom'])){
	$smarty->assign('rukuDateFrom', $_GET['rukuDateFrom']);
	$_GET['rukuDateFrom'] = trim($_GET['rukuDateFrom']);
}
if(isset($_GET['rukuDateTo'])){
	$smarty->assign('rukuDateTo', $_GET['rukuDateTo']);
	$_GET['rukuDateTo'] = trim($_GET['rukuDateTo']);
}
if(isset($_GET['chukuDateFrom'])){
	$smarty->assign('chukuDateFrom', $_GET['chukuDateFrom']);
	$_GET['chukuDateFrom'] = trim($_GET['chukuDateFrom']);
}

if(isset($_GET['chukuDateTo'])){
	$smarty->assign('chukuDateTo', $_GET['chukuDateTo']);
	$_GET['chukuDateTo'] = trim($_GET['chukuDateTo']);
}

if(isset($_GET['shipmentDateFrom'])){
	$smarty->assign('shipmentDateFrom', $_GET['shipmentDateFrom']);
	$_GET['shipmentDateFrom'] = trim($_GET['shipmentDateFrom']);
}

if(isset($_GET['shipmentDateTo'])){
	$smarty->assign('shipmentDateTo', $_GET['shipmentDateTo']);
	$_GET['shipmentDateTo'] = trim($_GET['shipmentDateTo']);
}

if(isset($_GET['certificationCodification'])){
	$smarty->assign('certificationCodification', $_GET['certificationCodification']);
	$_GET['certificationCodification'] = trim($_GET['certificationCodification']);
}

if(isset($_GET['sequenceNumber'])){
	$smarty->assign('sequenceNumber', $_GET['sequenceNumber']);
	$_GET['sequenceNumber'] = Filter::forDBInsertion(trim($_GET['sequenceNumber']));
}
if(isset($_GET['manufactory'])){
	$smarty->assign('manufactory', $_GET['manufactory']);
	$_GET['manufactory'] = Filter::forDBInsertion(trim($_GET['manufactory']));
}

if(isset($_GET['authenticationMark'])){
	$smarty->assign('authenticationMark', $_GET['authenticationMark']);
	$_GET['authenticationMark'] = Filter::forDBInsertion(trim($_GET['authenticationMark']));
}

if(isset($_GET['steelNumber'])){
	$smarty->assign('steelNumber', $_GET['steelNumber']);
	$_GET['steelNumber'] = Filter::forDBInsertion(trim($_GET['steelNumber']));
}

if(isset($_GET['orderNumber'])){
	$smarty->assign('orderNumber', $_GET['orderNumber']);
	$_GET['orderNumber'] = Filter::forDBInsertion(trim($_GET['orderNumber']));
}

if(isset($_GET['orderSubitemNumber'])){
	$smarty->assign('orderSubitemNumber', $_GET['orderSubitemNumber']);
	$_GET['orderSubitemNumber'] = trim($_GET['orderSubitemNumber']);
}

if(isset($_GET['unitPrice'])){
	$smarty->assign('unitPrice', $_GET['unitPrice']);
	$_GET['unitPrice'] = trim($_GET['unitPrice']);
}

if(isset($_GET['materialNumber'])){
	$smarty->assign('materialNumber', $_GET['materialNumber']);
	$_GET['materialNumber'] = Filter::forDBInsertion(trim($_GET['materialNumber']));
}

if(isset($_GET['consignmentBatch'])){
	$smarty->assign('consignmentBatch', $_GET['consignmentBatch']);
	$_GET['consignmentBatch'] = Filter::forDBInsertion(trim($_GET['consignmentBatch']));
}

if(isset($_GET['filename'])){
	$smarty->assign('filename', $_GET['filename']);
	$_GET['filename'] = Filter::forDBInsertion(trim($_GET['filename']));
}
if(isset($_GET['remark'])){
	$smarty->assign('remark', $_GET['remark']);
	$_GET['remark'] = Filter::forDBInsertion(trim($_GET['remark']));
}

if(isset($_GET['sampleBatch'])){
	$smarty->assign('sampleBatch', $_GET['sampleBatch']);
	$_GET['sampleBatch'] = Filter::forDBInsertion(trim($_GET['sampleBatch']));
}
if(isset($_GET['purchaseNumber'])){
	$smarty->assign('purchaseNumber', $_GET['purchaseNumber']);
	$_GET['purchaseNumber'] = Filter::forDBInsertion(trim($_GET['purchaseNumber']));
}

if(isset($_GET['month'])){
	$smarty->assign('month', $_GET['month']);
	$_GET['month'] = Filter::forDBInsertion(trim($_GET['month']));
}
if(isset($_GET['vehicleNumber'])){
	$smarty->assign('vehicleNumber', $_GET['vehicleNumber']);
	$_GET['vehicleNumber'] = Filter::forDBInsertion(trim($_GET['vehicleNumber']));
}
if(isset($_GET['consignee'])){
	$smarty->assign('consignee', $_GET['consignee']);
	$_GET['consignee'] = Filter::forDBInsertion(trim($_GET['consignee']));
}
if(isset($_GET['handler'])){
	$smarty->assign('handler', $_GET['handler']);
	$_GET['handler'] = Filter::forDBInsertion(trim($_GET['handler']));
}

if(isset($_GET['ruku'])){
	$smarty->assign('ruku', $_GET['ruku']);
	$_GET['ruku'] = Filter::forDBInsertion(trim($_GET['ruku']));
}
if(isset($_GET['chuku'])){
	$smarty->assign('chuku', $_GET['chuku']);
	$_GET['chuku'] = Filter::forDBInsertion(trim($_GET['chuku']));
}
if(isset($_GET['main'])){
	$smarty->assign('main', $_GET['main']);
	$_GET['main'] = Filter::forDBInsertion(trim($_GET['main']));
}

$wheres = array();

if(!empty($_GET['certificationNumber'])){
	array_push($wheres, "certificationNumber like '%{$_GET['certificationNumber']}%'");
}


if(!empty($_GET['uploadTimeFrom']) && Verifier::isTIME($_GET['uploadTimeFrom'])){
	$time = date('Y/m/d H:i:s', strtotime($_GET['uploadTimeFrom']));
	array_push($wheres, "uploadTime >= '$time'");
}

if(!empty($_GET['uploadTimeTo']) && Verifier::isTIME($_GET['uploadTimeTo'])){
	$time = date('Y/m/d H:i:s', strtotime($_GET['uploadTimeTo']));
	array_push($wheres, "uploadTime <= '$time'");
}

if(!empty($_GET['filename'])){
	array_push($wheres, "filename like '%{$_GET['filename']}%'");
}

if(!empty($_GET['consignmentBatch'])){
	array_push($wheres, "consignmentBatch like '%{$_GET['consignmentBatch']}%'");
}

if(!empty($_GET['certificationCodification'])){
	array_push($wheres, "certificationCodification like '%{$_GET['certificationCodification']}%'");
}

if (empty($_GET['ruku']) and empty($_GET['chuku']) and empty($_GET['main'])){
	header('Location: advancedSearch.html');
}

$mainTable = true;
$rukuTable = true;
$chukuTable = true;
if(!empty($_GET['size1From']) or !empty($_GET['size1To']) or !empty($_GET['size2From']) or !empty($_GET['size2To']) or !empty($_GET['size3From']) or !empty($_GET['size3To']) 
	or !empty($_GET['remark']) or !empty($_GET['orderNumber']) or !empty($_GET['orderSubitemNumber']) or !empty($_GET['unitPrice']) or !empty($_GET['purchaseNumber'])
	or !empty($_GET['materialNumber']) or !empty($_GET['sequenceNumber']) or !empty($_GET['manufactory']) or !empty($_GET['authenticationMark']) 
	or !empty($_GET['steelNumber']) or !empty($_GET['sampleBatch'])
	or !empty($_GET['month']) or !empty($_GET['vehicleNumber']) or !empty($_GET['consignee']) or !empty($_GET['shipmentDateFrom']) or !empty($_GET['shipmentDateTo']) or !empty($_GET['facheNumber'])){
		$rukuTable = false;
		$chukuTable = false;
	}
if(!empty($_GET['rukuFrom']) or !empty($_GET['rukuTo'])){
	$mainTable = false;
	$chukuTable = false;
}
if(!empty($_GET['chukuTo']) or !empty($_GET['chukuTo']) or !empty($_GET['handler'])){
	$mainTable = false;
	$rukuTable = false;
}

if($mainTable){
	if(!empty($_GET['main'])){
	$mainWheres = $wheres;
	
	if(!empty($_GET['size1From']) && Verifier::isNUMBER($_GET['size1From'])){
		$size1From = doubleval($_GET['size1From']);
		array_push($mainWheres, "size1 >= {$size1From}");
	}
	
	if(!empty($_GET['size1To']) && Verifier::isNUMBER($_GET['size1To'])){
		$size1To = doubleval($_GET['size1To']);
		array_push($mainWheres, "size1 <= {$size1To}");
	}
	
	if(!empty($_GET['size2From']) && Verifier::isNUMBER($_GET['size2From'])){
		$size2From = doubleval($_GET['size2From']);
		array_push($mainWheres, "size2 >= {$size2From}");
	}
	
	if(!empty($_GET['size2To']) && Verifier::isNUMBER($_GET['size2To'])){
		$size2To = doubleval($_GET['size2To']);
		array_push($mainWheres, "size2 <= {$size2To}");
	}
	
	if(!empty($_GET['size3From']) && Verifier::isNUMBER($_GET['size3From'])){
		$size3From = doubleval($_GET['size3From']);
		array_push($mainWheres, "size3 >= {$size3From}");
	}
	
	if(!empty($_GET['size3To']) && Verifier::isNUMBER($_GET['size3To'])){
		$size3To = doubleval($_GET['size3To']);
		array_push($mainWheres, "size3 <= {$size3To}");
	}
	
	if(!empty($_GET['facheNumber'])){
		array_push($mainWheres, "facheNumber like '%{$_GET['facheNumber']}%'");
	}
	
	if(!empty($_GET['remark'])){
		array_push($mainWheres, "remark like '%{$_GET['remark']}%'");
	}
	
	if(!empty($_GET['orderNumber'])){
		array_push($mainWheres, "orderNumber like '%{$_GET['orderNumber']}%'");
	}
	
	if(!empty($_GET['orderSubitemNumber'])){
		array_push($mainWheres, "orderSubitemNumber = '{$_GET['orderSubitemNumber']}'");
	}
	
	if(!empty($_GET['unitPrice'])){
		array_push($mainWheres, "unitPrice = '{$_GET['unitPrice']}'");
	}
	
	if(!empty($_GET['purchaseNumber'])){
		array_push($mainWheres, "purchaseNumber like '%{$_GET['purchaseNumber']}%'");
	}
	
	if(!empty($_GET['materialNumber'])){
		array_push($mainWheres, "materialNumber like '%{$_GET['materialNumber']}%'");
	}
	
	if(!empty($_GET['sequenceNumber'])){
		array_push($mainWheres, "sequenceNumber like '%{$_GET['sequenceNumber']}%'");
	}
	
	if(!empty($_GET['manufactory'])){
		array_push($mainWheres, "manufactory like '%{$_GET['manufactory']}%'");
	}
	if(!empty($_GET['authenticationMark'])){
		array_push($mainWheres, "authenticationMark like '%{$_GET['authenticationMark']}%'");
	}
	
	if(!empty($_GET['steelNumber'])){
		array_push($mainWheres, "steelNumber = '{$_GET['steelNumber']}'");
	}
	
	if(!empty($_GET['sampleBatch'])){
		array_push($mainWheres, "sampleBatch like '%{$_GET['sampleBatch']}%'");
	}
	
	if(!empty($_GET['month'])){
		array_push($mainWheres, "month like '%{$_GET['month']}%'");
	}
	
	if(!empty($_GET['vehicleNumber'])){
		array_push($mainWheres, "vehicleNumber like '%{$_GET['vehicleNumber']}%'");
	}
	
	if(!empty($_GET['consignee'])){
		array_push($mainWheres, "consignee like '%{$_GET['consignee']}%'");
	}
	
	if(!empty($_GET['shipmentDateFrom']) || Verifier::isTIME($_GET['shipmentDateFrom'])){
		$time = date('Y/m/d', strtotime($_GET['shipmentDateFrom']));
		array_push($mainWheres, "shipmentDate >= '$time'");
	}

	if(!empty($_GET['shipmentDateTo']) || Verifier::isTIME($_GET['shipmentDateTo'])){
		$time = date('Y/m/d', strtotime($_GET['shipmentDateTo']));
		array_push($mainWheres, "shipmentDate <= '$time'");
	}
	$whereStr = join(' and ', $mainWheres);
//	var_dump($whereStr);

	if(count($mainWheres)==0){
		$query = "select *,(count * unitWeight) as weight from little_main";
	}else{
		$query = "select *,(count * unitWeight) as weight from little_main where $whereStr";
	}
	DB::query($query);

	$certificationNumber = array();
	
	$facheNumber = array();

	$certificationCodification = array();
	
	$sequenceNumber = array();
	 
	$manufactory = array();
	
	$authenticationMark = array();
	
	$steelNumber =  array();
	
	$size1 = array();
	
	$size2 = array();
	
	$size3 = array();
	
	$count = array();
	
	$unitWeight = array();
	
	$orderNumber = array();
	
	$orderSubitemNumber = array();
	
	$unitPrice = array();
	
	$materialNumber = array();
	
	$consignmentBatch = array();
	
	$sampleBatch = array();
	
	$purchaseNumber = array();
	
	$shipmentDate = array();
	
	$month = array();
	
	$vehicleNumber = array();
	
	$consignee = array();
	
	$remark = array();
	
	$filename = array();
	
	$uploadTime = array();
	
	$stat = array();
	
	$id = array();

	$result = DB::getResult();

	while($row = $result->fetch_assoc()){
			array_push($id, $row['id']);
			array_push($certificationNumber, $row['certificationNumber']);
			array_push($facheNumber, $row['facheNumber']);
			array_push($certificationCodification, $row['certificationCodification']);
			array_push($sequenceNumber, $row['sequenceNumber']);
			array_push($manufactory, $row['manufactory']);
			array_push($authenticationMark, $row['authenticationMark']);
			array_push($steelNumber, $row['steelNumber']);	
			array_push($size1, $row['size1']);
			array_push($size2, $row['size2']);
			array_push($size3, $row['size3']);
			array_push($count, $row['count']);
			array_push($unitWeight, $row['unitWeight']);			
			array_push($orderNumber, $row['orderNumber']);
			array_push($orderSubitemNumber, $row['orderSubitemNumber']);
			array_push($unitPrice,$row['unitPrice']);
			array_push($materialNumber, $row['materialNumber']);
			array_push($consignmentBatch, $row['consignmentBatch']);
			array_push($sampleBatch,$row['sampleBatch']);
			array_push($purchaseNumber,$row['purchaseNumber']);
			array_push($shipmentDate,$row['shipmentDate']);
			array_push($month,$row['month']);
			array_push($vehicleNumber, $row['vehicleNumber']);
			array_push($consignee, $row['consignee']);
			array_push($remark, $row['remark']);
			array_push($filename, $row['filename']);
			array_push($uploadTime, $row['uploadTime']);
			$query_chuku = "select certificationNumber from little_chuku where certificationNumber = '{$row['certificationNumber']}'";
			DB::query($query_chuku);
			$row_chuku = DB::getResult()->fetch_assoc();
			if(!empty($row['certificationNumber']) && isset($row['certificationNumber'])){
				if(count($row_chuku['certificationNumber'])>0){
					array_push($stat, '出库');
				}else{
					$query_ruku = "select certificationNumber from little_ruku 
					where certificationNumber = '{$row['certificationNumber']}'";
					DB::query($query_ruku);
					$row_ruku = DB::getResult()->fetch_assoc();
					if(count($row_ruku['certificationNumber'])>0){
						array_push($stat,'库中');
					}else{
						array_push($stat,'未动');
					}
				}
			}else 
				array_push($stat,'');
	}

	$smarty->assign(array(
				'main_id'	=> 	$id,
				'main_certificationNumber'	=> 	$certificationNumber,
				'main_facheNumber' => $facheNumber,
				'main_certificationCodification'		=> 	$certificationCodification,
				'main_sequenceNumber'		=> 	$sequenceNumber,
				'main_manufactory'		=> 	$manufactory,
				'main_authenticationMark'		=> 	$authenticationMark,
				'main_steelNumber'		=> 	$steelNumber,
				'main_size1'		=> 	$size1,
				'main_size2'		=> 	$size2,
				'main_size3'		=> 	$size3,
				'main_count'	=>	$count,
				'main_unitWeight'		=>	$unitWeight,
				'main_orderNumber'		=>	$orderNumber,
				'main_orderSubitemNumber'	=>	$orderSubitemNumber,
				'main_unitPrice'		=>	$unitPrice,
				'main_materialNumber'		=>  $materialNumber,
				'main_consignmentBatch'	=>  $consignmentBatch,
				'main_sampleBatch'		=>  $sampleBatch,
				'main_purchaseNumber'	=>  $purchaseNumber,
				'main_shipmentDate'	=>  $shipmentDate,
				'main_month'	=>  $month,
				'main_vehicleNumber'	=>  $vehicleNumber,
				'main_consignee'	=>  $consignee,
				'main_remark'	=>  $remark,
				'main_filename'	=>  $filename,
				'main_uploadTime'	=>  $uploadTime,
				'main_stat'	=>  $stat
				));
	}

}

$smarty->display ( 'simpleTableStat.html' );
?>